using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using MTRC.ODMS.BLL.COM;
using System.Xml;
using APJUtil = APJ.Util;
using System.IO;

namespace MTRC.ODMS.Web.SOM.Popup
{
    /// <summary>
    /// Author: Huangyao
    /// </summary>
    public partial class PopupSignature : BasePage_SOM
    {
        #region MenuID

        //protected override string MenuID
        //{
        //    get
        //    {
        //        return "M141501";
        //    }
        //}

        #endregion
        
        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(ChangeLanguage_Function);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.SetLanguage();
                this.SwitchSignatureAreaOrShowPic();
            }
        }

        private void SwitchSignatureAreaOrShowPic()
        {
            string fullFileName = "";
            //fullFileName = string.Concat(Request.QueryString["signatureFullFileName"]); //this can get the right value
            fullFileName = this.GetQueryStringValue("signatureFullFileName"); //huangyao 20090122 fixed TD:1156

            if (fullFileName == string.Empty)
            {
                this.pnlSignatureArea.Visible = true;
                this.pnlSignaturePic.Visible = false;
                this.SaveSignatureFile();
            }
            else
            {
                this.pnlSignatureArea.Visible = false;
                this.pnlSignaturePic.Visible = true;
                ShowSignaturePic(fullFileName);
            }
        }

        /// <summary>
        /// //huangyao 20090122 fixed TD:1156
        /// just can get [signatureFullFileName] value
        /// </summary>
        /// <param name="paramName"></param>
        /// <returns></returns>
        private string GetQueryStringValue(string paramName)
        { 
            string url = Request.UrlReferrer.Query;
            //string regexString = @"(^|\\?|&)" + paramName + @"=([^&]*)(\\s|&|$)";
            //string[] typeTables = System.Text.RegularExpressions.Regex.Split(url, regexString, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            //if (typeTables.Length > 0)
            //{
            //    return typeTables[0];
            //}
            //else
            //{
            //    return "";
            //}
            
            string returnValue = "";
            if (url.IndexOf(paramName) > -1)
            {
                returnValue = url.Substring(url.IndexOf(paramName) + paramName.Length + 1);
                returnValue = HttpUtility.UrlDecode(returnValue);
            }

            return returnValue;
        }

        //huangyao 20090122 fixed TD:1156--add a parameter [fullFileName]
        private void ShowSignaturePic(string fullFileName)
        {
            //edit by hujianhua 20100712 by issue 1430
            this.ReportDownloadPath = fullFileName;
            //modify by tim 20160406 for save signature into database
            //this.ViewState["GetPicURL"] = "~/SOM/Popup/GetPic.aspx?abc=" + "SignatureArrival";
            this.ViewState["GetPicURL"] = "~/SOM/Popup/GetPicNew.aspx?abc=" + "SignatureArrival";
            this.imgSignaturePic.ImageUrl = this.ViewState["GetPicURL"].ToString();
        }

        #endregion

        private void SaveSignatureFile()
        {
            if (Request.Form["rgb_array"] != null)
            {
                string bmpSource = Request.Form["rgb_array"];

                int photow = 250;
                int photoh = 150;

                string[] rgbArray = bmpSource.Split(new char[] { ',' });
                //string path = HttpContext.Current.Server.MapPath("~/images/1.bmp");
                string fullFileName = APJ.Util.Utility.GetAppSetting("SOM_SignatureFileTempPath") + "Signature_Handwritten_" + Guid.NewGuid().ToString() + ".gif";
                this.DrawImageFromFlashUploader(photow, photoh, rgbArray).Save(fullFileName, System.Drawing.Imaging.ImageFormat.Gif);
                
                 Session[MTRC.ODMS.BLL.COM.Session.SOM_SignatureFullFileName_Arrival] = fullFileName;
                 this.hdnTempValue.Value = fullFileName;
                 ViewState[MTRC.ODMS.BLL.COM.Session.SOM_IsNeedSignAgain] = false;
            }
        }

        private System.Drawing.Bitmap DrawImageFromFlashUploader(int width, int height, string[] rgbArray)
        {
            System.Drawing.Bitmap bm = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            int pos = 0;
            for (int i = bm.Height - 1; i >= 0; i--)
            {
                for (int j = 0; j < bm.Width; j++)
                {
                    bm.SetPixel(j, i, System.Drawing.Color.FromArgb(int.Parse(rgbArray[pos], System.Globalization.NumberStyles.HexNumber)));
                    pos++;
                }
            }

            return bm;
        }

        #region SetLanguage

        void ChangeLanguage_Function(object sender, EventArgs e)
        {
            SetLanguage();
        }

        private void SetLanguage()
        {
            this.btnClear.Text = this.GetLabel("SOM_SignType_ClearOrignalSignPic");
        }

        #endregion

        protected void btnClear_Click(object sender, EventArgs e)
        {
            //huangyao 20090122 fixed TD:1156
            //this.pnlSignatureArea.Visible = true;
            //this.pnlSignaturePic.Visible = false;
            ////clear session
            //Session[MTRC.ODMS.BLL.COM.Session.SOM_SignatureFullFileName_Arrival] = null;
            //this.hdnTempValue.Value = "";
            //------------//huangyao 20090122 fixed TD:1156
        }

        //protected void hdnEventSignType_ServerChange(object sender, EventArgs e)
        //{
        //    string ss = "";
        //    string eventSignType = this.hdnEventSignType.Value.Trim();
        //}
    }
}
